<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="服用药品" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['takingMedicine']" placeholder="请输入服用药品"></a-input>
        </a-form-item>
        <a-form-item label="服药时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择服药时间" v-decorator="['medicationTime']" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="下次检查时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择下次检查时间" v-decorator="['nextInspectionTime']" :trigger-change="true"
                  style="width: 100%"/>
        </a-form-item>
        <a-form-item label="服务记录图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload text="服务记录图片"
                    :number="6"
                    v-decorator="['serviceRecordImage']"
                    fileType="image"/>
        </a-form-item>
        <a-form-item label="服务记录" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['serviceRecord',validatorRules.serviceRecord]" placeholder="请输入服务记录"></a-textarea>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import ATextarea from "ant-design-vue/es/input/TextArea";
  import JUpload from '@/components/jeecg/JUpload'


  export default {
    name: "MedicalServiceOrderModal",
    components: {
      ATextarea,
      JUpload,
      JDate,
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {
          serviceRecord: {
            rules: [{required: true, message: '请输入服务记录!'},]
          },
        },
        url: {
          add: "/order/medicalServiceOrder/add",
          edit: "/order/medicalServiceOrder/edit",
          serviceComplete: "/order/medicalServiceOrder/serviceComplete",
        },
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'userId', 'takingMedicine', 'medicationTime', 'nextInspectionTime', 'serviceRecordImage', 'serviceRecord', 'parentId', 'parentId', 'serviceId', 'hospitalId', 'orderSn', 'treatmentTime', 'patientId', 'telephone', 'escortId', 'payAmount', 'payType', 'payStatus', 'payTime', 'status', 'serviceStartTime', 'serviceEndTime', 'profitRatio', 'commissionRatio', 'profitAmount', 'commissionAmount', 'allocationStatus', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            let param = {
              orderId: formData.id,
              takingMedicine: formData.takingMedicine,
              medicationTime: formData.medicationTime,
              nextInspectionTime: formData.nextInspectionTime,
              serviceRecordImage: formData.serviceRecordImage,
              serviceRecord: formData.serviceRecord,
            }
            httpAction(that.url.serviceComplete, param, 'post').then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'userId', 'takingMedicine', 'medicationTime', 'nextInspectionTime', 'serviceRecordImage', 'serviceRecord', 'parentId', 'serviceId', 'hospitalId', 'orderSn', 'treatmentTime', 'patientId', 'telephone', 'escortId', 'payAmount', 'payType', 'payStatus', 'payTime', 'status', 'serviceStartTime', 'serviceEndTime', 'profitRatio', 'commissionRatio', 'profitAmount', 'commissionAmount', 'allocationStatus', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag'))
      },


    }
  }
</script>